package com.bjb.vr.auth.controller;

import com.bjb.vr.auth.service.UmsAccountEnhanceService;
import com.bjb.vr.common.ao.*;
import com.bjb.vr.common.dto.UmsAccount;
import com.bjb.vr.common.dto.UmsRole;
import com.bjb.vr.common.result.BaseErrorCode;
import com.bjb.vr.common.result.BaseResult;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.service.UmsAccountService;
import com.bjb.vr.common.service.UmsUserRoleService;
import com.bjb.vr.common.vo.PersonalInfoVo;
import com.bjb.vr.common.vo.UmsAccountVo;
import com.bjb.vr.common.vo.UmsRoleVo;
import com.bjb.vr.common.vo.UmsUserRoleVo;
import io.jsonwebtoken.lang.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * 教师端用户管理列表
 *
 * @author hujingbo
 * @version 1.0.0
 * @Description 登录认证接口列表
 * @createTime 2022年03月04日 11:01
 * @desc 登录认证接口列表
 */
@Slf4j
@RestController
@RequestMapping("/api/auth/v1")
public class AccountController {

    @Autowired
    private UmsAccountService umsAccountService;

    @Autowired
    private UmsAccountEnhanceService umsAccountEnhanceService;

    @Autowired
    private UmsUserRoleService umsUserRoleService;

    /**
     * 新增用户
     *
     * @param umsAccountAo
     * @return
     */
    @PostMapping("/addUser")
    public CommonResult addUser(@RequestBody UmsAccountAo umsAccountAo) {
        // 查询用户名或者昵称是否存在
        List<UmsAccount> umsAccount = umsAccountService.queryUserInfoByNameOrNickName(umsAccountAo);
        if (!CollectionUtils.isEmpty(umsAccount)){
            return CommonResult.Fail("用户账号或者姓名已存在");
        }
        // 新增用户
        boolean success = umsAccountService.saveUser(umsAccountAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_ADD);
        }
        return CommonResult.Ok();
    }

    /**
     * 查询用户信息
     *
     * @return
     */
    @PostMapping("/queryUser")
    public CommonResult queryUser(@Valid @RequestBody UmsUserRoleAo umsUserRoleAo) {
        List<UmsUserRoleVo> userRoleVos = umsUserRoleService.queryUserRoleInfo(umsUserRoleAo);
        return CommonResult.Ok().setResult(userRoleVos);
    }

    /**
     * 查询账户信息
     *
     * @param umsAccountAo
     * @return
     */
    @PostMapping("/queryAccount")
    public CommonResult queryAccount(@RequestBody UmsAccountAo umsAccountAo) {
        UmsAccountVo accountInfo = umsAccountService.getAccountInfo(umsAccountAo);
        return CommonResult.Ok().setResult(accountInfo);
    }

    /**
     * 更新用户状态
     *
     * @param statusAo
     * @return
     */
    @PostMapping("/updateUserStatus")
    public CommonResult updateUserStatus(@RequestBody UmsAccountStatusAo statusAo) {
        Assert.notNull(statusAo.getId(), "用户ID不能为空");
        // 禁止更新超级管理员
        boolean confirm = umsAccountEnhanceService.checkSuperAdmin(statusAo.getId(), null);
        if(confirm){
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE_SUPER_ADMIN);
        }
        boolean success = umsAccountEnhanceService.updateUserStatus(statusAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
        }
        return CommonResult.Ok();
    }

    /**
     * 更新用户密码（用户管理）
     *
     * @param accountPwdAo
     * @return
     */
    @PostMapping("/updateUserPassword")
    public CommonResult updateUserPassword(@RequestBody UmsAccountPwdAo accountPwdAo) {
        boolean success = umsAccountEnhanceService.updateUserPassword(accountPwdAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
        }
        return CommonResult.Ok();
    }

    /**
     * 重置用户密码（个人中心）
     *
     * @param accountResetPwdAo
     * @return
     */
    @PostMapping("/resetUserPassword")
    public BaseResult updateUserPassword(@RequestHeader String authorization, @RequestBody UmsAccountResetPwdAo accountResetPwdAo) {
        Assert.notNull(authorization, "请求头不能为空");
        Assert.notNull(accountResetPwdAo.getOldPassword(), "旧密码不能为空");
        Assert.notNull(accountResetPwdAo.getNewPassword(), "新密码不能为空");
        BaseResult baseResult = umsAccountEnhanceService.verifyUserNameAndPassword(authorization, accountResetPwdAo);
        if (!baseResult.isSuccess()) {
            return CommonResult.Fail("原密码输入错误！");
        }
        // 修改密码
        boolean success = umsAccountEnhanceService.updateUserPassword((UmsAccountPwdAo) baseResult.getData());
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
        }
        return CommonResult.Ok();
    }

    /**
     * 重置用户密码（客户管理）
     *
     * @param accountResetPwdAo
     * @return
     */
    @PostMapping("/resetCustomerPassword")
    public BaseResult resetCustomerPassword(@RequestBody UmsAccountResetPwdAo accountResetPwdAo) {
        Assert.notNull(accountResetPwdAo.getNewPassword(), "新密码不能为空");
        UmsAccountPwdAo accountPwdAo = new UmsAccountPwdAo();
        accountPwdAo.setPassword(accountResetPwdAo.getNewPassword());
        // 修改密码
        boolean success = umsAccountEnhanceService.updateCustomerPassword(accountPwdAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
        }
        return CommonResult.Ok();
    }

    /**
     * 更新用户角色
     *
     * @param umsAccountAo
     * @return
     */
    @PostMapping("/updateUser")
    public CommonResult updateUser(@RequestBody UmsAccountAo umsAccountAo) {
        Assert.notNull(umsAccountAo.getUserId(), "用户ID不能为空");
        // 禁止更新超级管理员
        boolean confirm = umsAccountEnhanceService.checkSuperAdmin(umsAccountAo.getUserId(), null);
        if(confirm){
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE_SUPER_ADMIN);
        }

        boolean success = umsAccountEnhanceService.updateUserInfo(umsAccountAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
        }
        return CommonResult.Ok();
    }

    /**
     * 查询角色信息
     *
     * @return
     */
    @PostMapping("/queryRole")
    public CommonResult queryRole(@RequestBody UmsUserRoleAo userRoleAo) {
        List<UmsRoleVo> umsRoleVos = umsAccountEnhanceService.queryRoleInfo(userRoleAo);
        return CommonResult.Ok().setResult(umsRoleVos);
    }

    /**
     * 添加角色
     *
     * @return
     */
    @PostMapping("/addRole")
    public CommonResult addRole(@RequestBody UmsRoleAo umsRoleAo) {
        Assert.notNull(umsRoleAo.getRoleName(), "角色名称不能为空");
        Assert.notNull(umsRoleAo.getMenuIds(), "菜单ID不能为空");
        Assert.notNull(umsRoleAo.getType(), "菜单类型不能为空");
        // 查询角色名称是否存在
        UmsRole umsRole = umsAccountEnhanceService.queryRoleByName(umsRoleAo.getRoleName());
        if (Objects.nonNull(umsRole)){
            return CommonResult.Fail("角色名称已存在");
        }
        // 添加角色
        boolean success = umsAccountEnhanceService.addRoleInfo(umsRoleAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_ADD);
        }
        return CommonResult.Ok("新增成功");
    }

    /**
     * 更新角色
     *
     * @return
     */
    @PostMapping("/updateRole")
    public CommonResult updateRole(@RequestBody UmsRoleAo umsRoleAo) {
        Assert.notNull(umsRoleAo.getRoleId(), "角色ID不能为空");
        boolean success = umsAccountEnhanceService.updateRole(umsRoleAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_ADD);
        }
        return CommonResult.Ok("更新成功");
    }

    /**
     * 个人中心
     *
     * @return
     */
    @PostMapping("/personalInfo")
    public CommonResult queryPersonalInfo() {
        PersonalInfoVo personalInfo = umsAccountEnhanceService.queryPersonalInfo();
        return CommonResult.Ok().setResult(personalInfo);
    }

}

